// 用户登录日志
<template>
  <div class="loginlog-container">
    <el-row class="bread-nav">
      <ul class="bread-nav-title">
        <span>登录日志</span>
      </ul>
    </el-row>
    <!--<div style="margin-bottom:8px;">-->
      <!--<el-tag type="warning">账号:{{this.params.username}}</el-tag>-->
    <!--</div>-->
    <div class="elTable">
      <el-table :data="list" v-loading="loading" element-loading-text="加载中" size="small" header-cell-class-name='tableHeader' border stripe fit highlight-current-row>
        <el-table-column type="index" label="序号" align="center" min-width="50"> </el-table-column>
        <!--<el-table-column prop="index" label="序号" align="center" min-width="80"> </el-table-column>-->
        <el-table-column v-if="params.username === null" prop="username" label="账号" align="center" min-width="80"> </el-table-column>
        <el-table-column label="登录时间" align="center" min-width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ format(scope.row.created_at) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="IP" align="center" min-width="80"> </el-table-column>
        <el-table-column prop="data" label="IP归属地" align="center" min-width="180"> </el-table-column>
      </el-table>
      <div class="pagination-container" v-if="pagination.totalCount > 0">
        <el-pagination @size-change="handlePageSizeChange" @current-change="handleCurrentChange" :current-page="pagination.currentPage" :page-sizes="[10,20,30,50]" :page-size="params.perpage" :total="pagination.totalCount" layout="total,sizes,prev,pager,next,jumper" background>
        </el-pagination>
      </div>
      <div style="margin-top:8px">
        <el-tag type="info" style="width:100%">注意:登录日志最少被保留7天,超过7天部分最多保留最后20条.</el-tag>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 查詢參數
      params: {
        username: null,
        perpage: 10, // 每页数量
      },
      // 分页信息
      pagination: {
        currentPage: 1,
        totalCount: 0,
        defaultPageSize: 10,
      },
      list: null,
      loading: true,
    }
  },
  filters: {},
  computed: {
    ...mapGetters(['info']),
  },
  created() {
    this.checkParams(this.$route.params)
    // 拉取数据
    this.fetchData()
  },
  methods: {
    add0(m) {
      return m < 10 ? '0' + m : m
    },
    format(shijianchuo) {
      // shijianchuo是整数，否则要parseInt转换
      const time = new Date(parseInt(shijianchuo))
      const y = time.getFullYear()
      const m = time.getMonth() + 1
      const d = time.getDate()
      const h = time.getHours()
      const mm = time.getMinutes()
      const s = time.getSeconds()
      return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s)
    },
    checkParams(params) {
      if (params && params.username && params.username !== ':username') {
        this.params.username = params.username
      } else {
        this.params.username = this.info.username
      }
    },
    // 获取数据
    fetchData() {
      this.loading = true
      this.$store
        .dispatch('ListLoginHists', {
          params: this.params, // 路由参数
          pagination: this.pagination,
        })
        .then(response => {
          this.loading = false
          console.log('登录日志获取数据', response)
          this.list = response.data.slice(0, 20)
        })
    },

    // 当前页变化
    handleCurrentChange(value) {
      this.pagination.currentPage = value
      this.fetchData()
    },

    // 每页显示数量变化
    handlePageSizeChange(value) {
      this.params.perpage = value
      this.fetchData()
    },
  },
  watch: {
    $route(to, from) {
      // 监控路由变化,从新加载数据
      if (to.params !== from.params) {
        this.checkParams(to.params)
        if (this.pagination.currentPage !== 1) {
          this.pagination.currentPage = 1 // 默认查询第一页,当前页变化,会触发数据拉取
        } else {
          this.fetchData()
        }
      }
    },
  },
}
</script>
<style>
  .bread-nav{
    height:30px;
    font-size: 13px;
    line-height: 30px;
    background: #ECEFF2;
    border-bottom: 1px solid #d0dff2;
    padding: 0 0 0 10px;
    overflow: hidden;
  }
  .bread-nav-title{
    color:#386399;
    font-weight: bold;
    background: url("../../../../static/agent/img/tb.png") no-repeat left center;
    padding-left: 20px;
  }
  .elTable{
    padding: 10px;
  }
</style>

